package com.liangsui.mss.controller.doctor;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.*;
import com.liangsui.mss.dto.PatientDto;
import com.liangsui.mss.dto.RegistrationDto;
import com.liangsui.mss.dto.RegistrationFormDto;
import com.liangsui.mss.dto.RegistrationQueryDto;
import com.liangsui.mss.service.DeptService;
import com.liangsui.mss.service.PatientService;
import com.liangsui.mss.service.RegistrationService;
import com.liangsui.mss.service.SchedulingService;
import com.liangsui.mss.utils.IdGeneratorSnowflake;
import com.liangsui.mss.utils.ShiroSecurityUtils;
import com.liangsui.mss.vo.AjaxResult;
import com.liangsui.mss.vo.DataGridView;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * RegistrationController
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/12/7 18:11
 */
@RestController
@RequestMapping("doctor/registration")
public class RegistrationController {

    @DubboReference
    private RegistrationService registrationService;

    @DubboReference
    private SchedulingService schedulingService;

    @DubboReference
    private PatientService patientService;

    @Autowired
    private DeptService deptService;

    /**
     * 分页查询挂号信息
     */
    @GetMapping("queryRegistrationForPage")
    public AjaxResult queryRegistrationForPage(RegistrationDto registrationDto) {
        DataGridView gridView = this.registrationService.queryRegistrationForPage(registrationDto);
        return AjaxResult.success("查询成功", gridView.getData(), gridView.getTotal());
    }

    /**
     * 根据患者ID查挂号信息（小程序）
     * @param patientId
     * @return
     */
    @GetMapping("queryRegistrationByPatientId")
    public AjaxResult getRegistrationsByPatientId(String patientId) {
        List<Registration> registrations = this.registrationService.getRegistrationsByPatientId(patientId);
        return AjaxResult.success("查询成功", registrations);
    }

    @GetMapping("selectAll")
    public AjaxResult selectAll() {
        List<Scheduling> schedulings = this.schedulingService.selectAll2();
        return AjaxResult.success("查询成功", schedulings);
    }

    /**
     * 添加挂号信息
     *
     * @param registrationFormDto
     * @return
     */
    @PostMapping("addRegistration")
    public AjaxResult addRegistration(@RequestBody @Validated RegistrationFormDto registrationFormDto) {
        PatientDto patientDto = registrationFormDto.getPatientDto();
        RegistrationDto registrationDto = registrationFormDto.getRegistrationDto();
        Patient patient = null;
        //保存患者信息
        if (StringUtils.isBlank(patientDto.getPatientId())) {
            patientDto.setPatientId(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PREFIX_HZ));
            patientDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
            patient = patientService.addPatient(patientDto);
        } else {
            patient = this.patientService.getPatientById(patientDto.getPatientId());
        }
        if (null == patient) {
            return AjaxResult.fail("当前患者ID不存在，请确认后再提交");
        }
        //修改部门挂号
        Dept dept = deptService.getDeptById(registrationDto.getDeptId());
        //保存挂号信息
        registrationDto.setRegId(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PREFIX_GH));
        registrationDto.setPatientId(patient.getPatientId());
        registrationDto.setPatientName(patient.getName());
        registrationDto.setRegNumber(dept.getRegNumber() + 1);
        registrationDto.setVisitDate(registrationDto.getVisitDate().substring(0, 10));
        registrationDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        registrationDto.setSubsectionType(registrationDto.getSubsectionType());
        this.registrationService.addRegistration(registrationDto);
        //修改部门挂号信息编号
        dept.setRegNumber(dept.getRegNumber() + 1);
        this.deptService.updateDeptRegNumber(dept.getDeptId(), dept.getRegNumber());
        //返回挂号编号
        return AjaxResult.success("", registrationDto.getRegId());
    }

    /**
     * 小程序挂号
     * @param registrationFormDto
     * @return
     */
    @PostMapping("minAddRegistration")
    public AjaxResult miniAddRegistration(@RequestBody @Validated RegistrationFormDto registrationFormDto) {
        PatientDto patientDto = registrationFormDto.getPatientDto();
        RegistrationDto registrationDto = registrationFormDto.getRegistrationDto();
        Patient patient = null;
        //保存患者信息
        if (StringUtils.isBlank(patientDto.getPatientId())) {
            patientDto.setPatientId(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PREFIX_HZ));
            patientDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
            patient = patientService.addPatient(patientDto);
        } else {
            patient = this.patientService.getPatientById(patientDto.getPatientId());
        }
        if (null == patient) {
            return AjaxResult.fail("当前患者ID不存在，请确认后再提交");
        }
        //修改部门挂号
        Dept dept = deptService.getDeptById(registrationDto.getDeptId());
        //保存挂号信息
        registrationDto.setRegId(IdGeneratorSnowflake.generatorIdWithProfix(Constants.ID_PREFIX_GH));
        registrationDto.setPatientId(patient.getPatientId());
        registrationDto.setPatientName(patient.getName());
        registrationDto.setRegNumber(dept.getRegNumber() + 1);
        registrationDto.setVisitDate(registrationDto.getVisitDate().substring(0, 10));
        registrationDto.setSimpleUser(new SimpleUser(patient.getPatientId(), patient.getName()));
        registrationDto.setSubsectionType(registrationDto.getSubsectionType());
        this.registrationService.addRegistration(registrationDto);
        //修改部门挂号信息编号
        dept.setRegNumber(dept.getRegNumber() + 1);
        this.deptService.updateDeptRegNumber(dept.getDeptId(), dept.getRegNumber());
        //返回挂号编号
        return AjaxResult.success("", registrationDto.getRegId());
    }

    /**
     * 查询可用的科室
     * 从his_scheduling表中查询
     */
    @GetMapping("listDeptForScheduling")
    public AjaxResult listDeptForScheduling(RegistrationQueryDto registrationDto) {
        Long deptId = registrationDto.getDeptId();
        String subsectionType = registrationDto.getSubsectionType();
        String schedulingType = registrationDto.getSchedulingType();
        String schedulingDay = registrationDto.getSchedulingDay();
        List<Long> deptIds = this.schedulingService.queryDeptIdsForScheduling(deptId, subsectionType, schedulingType, schedulingDay);
        if (deptIds == null || deptIds.size() == 0) {
            return AjaxResult.success(Collections.EMPTY_LIST);
        } else {
            List<Dept> depts = this.deptService.listDeptByDeptIds(deptIds);
            return AjaxResult.success(depts);
        }
    }

    /**
     * 挂号收费
     */
    @PutMapping("collectFee/{regId}")
    public AjaxResult collectFee(@PathVariable String regId) {
        Registration registration = this.registrationService.queryRegistrationByRegId(regId);
        if (null == registration) {
            return AjaxResult.fail("当前挂号单ID对应的挂号单不存在，请核对后再查询");
        }
        if (!registration.getRegStatus().equals(Constants.REG_STATUS_0)) {
            return AjaxResult.fail("当前挂号单状态不是未收费状态，不能收费");
        }
        registration.setRegStatus(Constants.REG_STATUS_1);
        return AjaxResult.toAjax(this.registrationService.updateRegistrationById(registration));
    }

    /**
     *
     * 挂号退费
     */
    @PutMapping("doReturn/{regId}")
    public AjaxResult doReturn(@PathVariable String regId) {
        Registration registration = this.registrationService.queryRegistrationByRegId(regId);
        if (null == registration) {
            return AjaxResult.fail("当前挂号单ID对应的挂号单不存在，请核对后再查询");
        }
        if (!registration.getRegStatus().equals(Constants.REG_STATUS_1)) {
            return AjaxResult.fail("当前挂号单状态不是收费状态，不能退费");
        }
        registration.setRegStatus(Constants.REG_STATUS_4);
        return AjaxResult.toAjax(this.registrationService.updateRegistrationById(registration));
    }

    /**
     * 挂号作废
     */
    @PutMapping("doInvalid/{regId}")
    public AjaxResult doInvalid(@PathVariable String regId) {
        Registration registration = this.registrationService.queryRegistrationByRegId(regId);
        if (null == registration) {
            return AjaxResult.fail("当前挂号单ID对应的挂号单不存在，请核对后再查询");
        }
        if (!registration.getRegStatus().equals(Constants.REG_STATUS_4)) {
            return AjaxResult.fail("当前挂号单状态不是退费状态，不能作废");
        }
        registration.setRegStatus(Constants.REG_STATUS_5);
        return AjaxResult.toAjax(this.registrationService.updateRegistrationById(registration));
    }

    /**
     * 根据身份证号查询患者信息
     */
    @GetMapping("getPatientByIdCard/{idCard}")
    public AjaxResult getPatientByIdCard(@PathVariable String idCard) {
        Patient patient = this.patientService.getPatientByIdCard(idCard);
        if (null == patient) {
            return AjaxResult.fail(idCard + "对应的患者不存在，请在下面新建患者信息");
        } else {
            return AjaxResult.success(patient);
        }
    }
}
